package bupt.party.online.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import bupt.party.online.model.LearnRecord;
import bupt.party.online.model.PartyNews;
import bupt.party.online.model.Student;
import bupt.party.online.service.IAdminService;
import bupt.party.online.service.IBranchService;
import bupt.party.online.service.ILearnRecordService;
import bupt.party.online.service.IStudentService;
import bupt.party.online.util.DateUtil;
import bupt.party.online.util.SessionConstants;
import bupt.party.online.vo.Nationality;

public class UserAction extends Pagination implements ServletRequestAware,
		ServletResponseAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 验证发生错误的时候，返回给前台界面的提示信息。
	private String result = "";
	// 用户的身份
	private String userstatus = "0";// 0为游客 ；1为学生； 2为管理员
	// 登录的用户名,
	private String userid;
	// 登录的密码
	private String password;
	// 验证码
	private String yanzhengma;

	private String oldPassword;

	private String newPassword;

	private String newPassword2;
	
	private String stuName ;
	private String stuNO ;
	private String sfzNO;
	private String stuGender;
	private String stuNationality;
	private String telephone;
	private String email;
	
	//分页
	private String paginationCode;
	private List<LearnRecord> currentLearnRecordList;

	private HttpServletRequest request;
	private HttpServletResponse response;

	private IStudentService studentService;
	private IAdminService adminService;
	private ILearnRecordService learnRecordService;
	private IBranchService branchService;
	
	
	private Nationality nationality;

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public IStudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public IAdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	public ILearnRecordService getLearnRecordService() {
		return learnRecordService;
	}

	public void setLearnRecordService(ILearnRecordService learnRecordService) {
		this.learnRecordService = learnRecordService;
	}

	public IBranchService getBranchService() {
		return branchService;
	}

	public void setBranchService(IBranchService branchService) {
		this.branchService = branchService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUserstatus() {
		return userstatus;
	}

	public void setUserstatus(String userstatus) {
		this.userstatus = userstatus;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getYanzhengma() {
		return yanzhengma;
	}

	public void setYanzhengma(String yanzhengma) {
		this.yanzhengma = yanzhengma;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getNewPassword2() {
		return newPassword2;
	}

	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}
	

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getStuNO() {
		return stuNO;
	}

	public void setStuNO(String stuNO) {
		this.stuNO = stuNO;
	}

	public String getSfzNO() {
		return sfzNO;
	}

	public void setSfzNO(String sfzNO) {
		this.sfzNO = sfzNO;
	}

	public String getStuGender() {
		return stuGender;
	}

	public void setStuGender(String stuGender) {
		this.stuGender = stuGender;
	}

	public String getStuNationality() {
		return stuNationality;
	}

	public void setStuNationality(String stuNationality) {
		this.stuNationality = stuNationality;
	}

	

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	

	public Nationality getNationality() {
		return nationality;
	}

	public void setNationality(Nationality nationality) {
		this.nationality = nationality;
	}

	

	public String getPaginationCode() {
		return paginationCode;
	}

	public void setPaginationCode(String paginationCode) {
		this.paginationCode = paginationCode;
	}


	public List<LearnRecord> getCurrentLearnRecordList() {
		return currentLearnRecordList;
	}

	public void setCurrentLearnRecordList(List<LearnRecord> currentLearnRecordList) {
		this.currentLearnRecordList = currentLearnRecordList;
	}

	// 进入个人中心UI
	public String personCenter() {
		HttpSession session = request.getSession();
		Student session_student = (Student)session.getAttribute(SessionConstants.SESSION_USER);
		Student  student = studentService.findStudentByStudentNO(session_student.getStudentNo());
		
		int branchId = student.getStudentBranchId();
		String branchName = branchService.getBranchName(branchId);
		//将学生信息传给前台界面
		request.setAttribute("student", student);
		request.setAttribute("branchName", branchName);
		return "personCenter";
	}

	// 查改个人信息UI
	public String personInfo() {
		HttpSession session = request.getSession();
		Student session_student = (Student)session.getAttribute(SessionConstants.SESSION_USER);
		Student  student = studentService.findStudentByStudentNO(session_student.getStudentNo());
		
		
		int branchId = student.getStudentBranchId();
		String branchName = branchService.getBranchName(branchId);
		//将学生信息传给前台界面
		request.setAttribute("branchName", branchName);
		request.setAttribute("student", student);
		
		return "personInfo";
	}

	//保存个人信息
	public String save_personInfo(){
		//根据studentNo先查询对象(7个字段)
		
		Student  student = studentService.findStudentByStudentNO(stuNO);
		
		student.setStudentEmail(email);
		student.setStudentGender(stuGender);
		student.setStudentIdentity(sfzNO);
		student.setStudentNationality(stuNationality);
		student.setStudentTelephone(telephone);
		//保存
		studentService.saveOrUpdateStudent(student);
		
		return "save_personInfo";
	}
	
	
	// 修改个人密码UI
	public String modifyPassword() {
		
		return "modifyPassword";
	}

	 //保存修改的密码
	 public String save_modifyPassword(){
	
	 HttpSession session = request.getSession();
	
	 Student session_student = (Student)session.getAttribute(SessionConstants.SESSION_USER);
	 
	 Student student = studentService.findStudentByStudentNO(session_student.getStudentNo());
	 //首先检查旧的密码是不是正确
	 if((student.getstudentPassword()).equals(oldPassword)){//旧的密码正确
		 
	 boolean bl =studentService.savePasswordByStudentNO(student.getStudentNo(),newPassword);
	
	 if(bl==true){
	 result = "密码修改成功！";
	 }else{
	 result = "密码修改失败！";
	 }
	
	 }else{//旧的密码错误
	 result = "提示：旧密码输入错误，请返回重新修改！";
	 }
	
	 request.setAttribute("result", result);
	
	 return "save_modifyPassword";
	 }
	
	 //分页查看学习记录UI
	 public String viewRecord(){
		 
		HttpSession session = request.getSession();
		Student session_student = (Student) session.getAttribute(SessionConstants.SESSION_USER);
		 
		if(this.pageNow == null ) pageNow = 1;
			
		totalResults = 0;
		
		paginationCode = "";
		
		totalResults = learnRecordService.getResultCount();//总的学习记录条数
		
		this.pageCount = (totalResults%pageSize == 0)?totalResults/pageSize : totalResults/pageSize + 1;//计算一下总页数
		
		
		currentLearnRecordList = this.learnRecordService.getLearnRecordByConditions(null, pageNow, pageSize);//目前conditions是空的。以后可能扩展到ip条件查询
		
		
		if(totalResults > 0) paginationCode = Pagination.getPaginationCode(pageSize, totalResults, pageNow);
		
		else paginationCode = "没有查询到有效数据！";
		 
		 
		//学习总时间长度是所有的记录的总和。
		double learnTimeSum = learnRecordService.getLearnTimeSumByStudentNo(session_student.getStudentNo());
		
		java.text.DecimalFormat   df  =  new   java.text.DecimalFormat( "#,##0.000"); 
		String  f_learnTimeSum = df.format(learnTimeSum);
		
		request.setAttribute("learnTimeSum", f_learnTimeSum);
		 
		return "viewRecord";
	 }
	 
	 
	 
	 
}
